/********************************************************************************
 * FrontIndexAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Dec 16, 2008 11:28:31 AM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.printeverywhere.delegator.BannerCampaignServiceDelegator;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.dto.BannerCampaigns;
import com.printeverywhere.dto.GeneralCategories;
import com.printeverywhere.dto.GeneralCategoriesWOS;
import com.printeverywhere.dto.Items;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.DateUtil;
import com.printeverywhere.util.SearchCriteria;

public class FrontIndexAction extends FrontBaseSupport implements ServletRequestAware {

	/**  */
	private static final long serialVersionUID = 1L;

	private HttpServletRequest request;

	private List<BannerCampaigns> bannerCampaignsList = new ArrayList<BannerCampaigns>();

	private List<BannerCampaigns> tempBannerCampaignsList = new ArrayList<BannerCampaigns>();

	private List<Items> featuredCompany = new ArrayList<Items>();

	private List<Items> featuredWantedItems = new ArrayList<Items>();

	private List<Items> featuredAdvertisement = new ArrayList<Items>();

	private List<Items> featuredFixedPriceItems = new ArrayList<Items>();

	private List<Items> featuredShops = new ArrayList<Items>();

	private List<Items> featuredItems = new ArrayList<Items>();

	private List<Items> featuredTender = new ArrayList<Items>();

	private SearchCriteria searchCriteria = new SearchCriteria();

	public FrontIndexAction() {
		super(true, true);
		setCategoryDisplay(true);
		setIndex(true);
	}

	@Override
	public String execute() throws Exception {
		try {
			List<Criterion> criterionList = new ArrayList<Criterion>();
			if(criterionList.size() > 0){
				criterionList.clear();
			}
			criterionList.add(Restrictions.gt("endDate", DateUtil.getTodaysDate()));
			criterionList.add(Restrictions.ge("startDate", DateUtil.getTodaysDate()));
			criterionList.add(Restrictions.eq("status", AdminConstants.TWO_NUMBER));
			criterionList.add(Restrictions.leProperty("clicks", "maxClicks"));
			criterionList.add(Restrictions.leProperty("days", "maxDays"));
			criterionList.add(Restrictions.leProperty("views", "maxViews"));
			if(request.getSession().getAttribute("categoryInSession") != null) {
				GeneralCategories category = (GeneralCategories) CommonFunctionDelegator.getObjectById("categoryId", Integer.parseInt(request.getSession().getAttribute("categoryInSession").toString()), GeneralCategories.class);
				criterionList.add(Restrictions.eq("generalCategories", category));
				bannerCampaignsList = (List<BannerCampaigns>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null,BannerCampaigns.class);
			} else {
				bannerCampaignsList = BannerCampaignServiceDelegator.getBannerCampaigns();
			}
			int left = 0;
			int right = 0;
			int center = 0;
			for (Iterator<BannerCampaigns> iterator = bannerCampaignsList.iterator(); iterator
					.hasNext();) {
				BannerCampaigns type = (BannerCampaigns) iterator.next();
				if(type.getBannerSettings().getNoOfFormat() > left && type.getBannerSettings().getId() == 1){
					if(type.getMaxViews() > 0 && type.getMaxViews() > type.getViews()){
						type.setViews(type.getViews() + 1);
					}
					left = left + 1;
				}
				if(type.getBannerSettings().getNoOfFormat() > right && type.getBannerSettings().getId() == 2){
					if(type.getMaxViews() > 0 && type.getMaxViews() > type.getViews()){
						type.setViews(type.getViews() + 1);
					}
					right = right + 1;
				}
				if(type.getBannerSettings().getNoOfFormat() > center && type.getBannerSettings().getId() == 3){
					if(type.getMaxViews() > 0 && type.getMaxViews() > type.getViews()){
						type.setViews(type.getViews() + 1);
					}
					center = center + 1;
				}
				CommonFunctionDelegator.generalUpdatebyobj(type);
			}
			if(criterionList.size() > 0){
				criterionList.clear();
			}
			criterionList.add(Restrictions.eq("homePageFeatured", Boolean.TRUE));
			criterionList.add(Restrictions.ge("endTime", DateUtil.getTodaysDate()));
			criterionList.add(Restrictions.eq("itemtype.id", 1));

			if(request.getSession().getAttribute("categoryInSession") != null) {
				GeneralCategoriesWOS category = (GeneralCategoriesWOS) CommonFunctionDelegator.getObjectById("categoryId", Integer.parseInt(request.getSession().getAttribute("categoryInSession").toString()), GeneralCategoriesWOS.class);
				List<Criterion> tmpCriterionList = new ArrayList<Criterion>();
				tmpCriterionList.add(Restrictions.ge("categoryId", category.getCategoryId()));
				tmpCriterionList.add(Restrictions.eq("mainParentId", category.getMainParentId()));
				criterionList.add(Restrictions.in("generalCategories", CommonFunctionDelegator.getObjectsByCriterion(tmpCriterionList, null, null, GeneralCategoriesWOS.class)));
			}

			this.featuredItems = (List<Items>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Items.class);

			criterionList.remove(2);
			criterionList.add(2, Restrictions.eq("itemtype.id", 2));

			this.featuredFixedPriceItems = (List<Items>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Items.class);

			criterionList.remove(3);
			criterionList.add(3, Restrictions.eq("itemtype.id", 3));

			this.featuredShops = (List<Items>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Items.class);

			criterionList.remove(4);
			criterionList.add(4, Restrictions.eq("itemtype.id", 4));

			this.featuredWantedItems = (List<Items>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Items.class);

			criterionList.remove(5);
			criterionList.add(5, Restrictions.eq("itemtype.id", 5));

			this.featuredAdvertisement = (List<Items>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Items.class);

			criterionList.remove(6);
			criterionList.add(6, Restrictions.eq("itemtype.id", 6));

			this.featuredCompany = (List<Items>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Items.class);

			criterionList.remove(7);
			criterionList.add(7, Restrictions.eq("itemtype.id", 7));

			this.featuredTender = (List<Items>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Items.class);

		} catch (Exception e) {
		}
		return SUCCESS;

	}

	public List<BannerCampaigns> getBannerCampaignsList() {
		return bannerCampaignsList;
	}

	public void setBannerCampaignsList(List<BannerCampaigns> bannerCampaignsList) {
		this.bannerCampaignsList = bannerCampaignsList;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public List<Items> getFeaturedItems() {
		return featuredItems;
	}

	public List<Items> getFeaturedCompany() {
		return featuredCompany;
	}

	public List<Items> getFeaturedWantedItems() {
		return featuredWantedItems;
	}

	public List<Items> getFeaturedFixedPriceItems() {
		return featuredFixedPriceItems;
	}

	public List<Items> getFeaturedShops() {
		return featuredShops;
	}

	public List<Items> getFeaturedAdvertisement() {
		return featuredAdvertisement;
	}

	public List<Items> getFeaturedTender() {
		return featuredTender;
	}
}
